package com.itheima.rabbitmq.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author : [chenxys]
 * @version : [v1.0]
 * @Description : 通配符模式 生产者 路由key必须包含.
 * @createTime : [2022/6/23 18:47]
 */
public class Producer {
    //交换机名称
    static final String TOPIC_EXCHAGE = "topic_exchange";
    //队列名称
    static final String TOPIC_QUEUE_1 = "topic_queue_1";
    //队列名称
    static final String TOPIC_QUEUE_2 = "topic_queue_2";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/heima");
        factory.setUsername("heima");
        factory.setPassword("heima");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(TOPIC_EXCHAGE, BuiltinExchangeType.TOPIC);

        channel.queueDeclare(TOPIC_QUEUE_1,true,false,false,null);
        channel.queueDeclare(TOPIC_QUEUE_2,true,false,false,null);

        //路由key组成 系统名称.日志级别
        //需求 队列一：所有的error级别的日志都存数据库,所有的order系统的日志都存数据库 #[0/多个]
        channel.queueBind(TOPIC_QUEUE_1,TOPIC_EXCHAGE,"#.error");
        channel.queueBind(TOPIC_QUEUE_1,TOPIC_EXCHAGE,"order.*");

        //队列2 ：所有信息都打印
        channel.queueBind(TOPIC_QUEUE_2,TOPIC_EXCHAGE,"*.*");

        String message1 = "你好,小兔子。开启topic --通配符模式 日志级别为info";

        channel.basicPublish(TOPIC_EXCHAGE,"goods.error",null,message1.getBytes());

        channel.close();
        connection.close();
    }
}
